import 'package:flutter/material.dart';
import 'package:wallet_home/tools/theme_utils.dart';
import 'app_bar_controller.dart';

class SearchAppBar extends StatelessWidget implements PreferredSizeWidget {
  final Color statusBarColor;
  final Color primary;
  final bool autoSelected;
  final AppBar mainAppBar;
  final Color mainTextColor;
  final String searchHint;
  final AppBarController appBarController;
  final Function(String search) onChange;
  final double searchFontSize;
  final Function(bool isSearch) onSearching;

  SearchAppBar({
    @required this.primary,
    this.mainTextColor = Colors.white,
    this.statusBarColor,
    this.autoSelected = false,
    this.searchHint = "enter search content",
    @required this.mainAppBar,
    @required this.appBarController,
    @required this.onChange,
    this.onSearching,
    this.searchFontSize = 16,
  });

  @override
  Size get preferredSize {
    return Size.fromHeight(60.0);
  }

  Widget build(BuildContext context) {
    // appBarController.stream.add(autoSelected);

    return StreamBuilder(
      stream: appBarController.stream.stream,
      builder: (BuildContext context, AsyncSnapshot<bool> snap) {
        bool _show = autoSelected;

        if (snap.hasData) {
          _show = snap.data;
        }
        print('ap.hasD -- $_show');

        // /// 搜索状态外抛
        // if (onSearching != null) {
        //   onSearching(_show);
        // }
        if (_show) {
          return searchAppBar(
            context: context,
          );
        } else {
          return showMainAppBar();
        }
      },
    );
  }

  Widget showMainAppBar() {
    return mainAppBar;
  }

  /// 选中样式
  Widget searchAppBar({@required BuildContext context}) {
    return AppBar(
      automaticallyImplyLeading: false,
      titleSpacing: 15.0,
      // leading: UnconstrainedBox(
      //   child: Container(
      //     width: 50.0,
      //     color: Colors.red,
      //     child: InkWell(
      //       child: Icon(
      //         Icons.close,
      //         color: mainTextColor,
      //       ),
      //       onTap: () {
      //         appBarController.stream.add(false);
      //         onChange('');
      //       },
      //     ),
      //   ),
      // ),
      backgroundColor: primary,
      title: Container(
        alignment: Alignment.center,
        // color:Colors.red,
        height: 30.0,
        decoration: BoxDecoration(
            color: ThemeUtils.getThemeColor(
                context: context,
                brightnessColor: Colors.white,
                darkColor: Colors.grey[400]),
            borderRadius: BorderRadius.all(Radius.circular(5))),
        // color: ThemeUtils.getThemeColor(
        //     context: context,
        //     brightnessColor: Colors.white,
        //     darkColor: Colors.grey[400]),
        child: TextField(
          textAlignVertical: TextAlignVertical.bottom,
          // textDirection: TextDirection.ltr,
          // textAlign: TextAlign.left,
          autofocus: true,
          onChanged: (String value) {
            onChange(value);
          },
          onEditingComplete: () {
            /// 搜索状态外抛
            if (onSearching != null) {
              onSearching(false);
            }
            appBarController.stream.add(false);
          },
          style: TextStyle(
            fontSize: searchFontSize,
            color: mainTextColor,
          ),
          cursorColor: mainTextColor,
          decoration: InputDecoration(
            hintText: searchHint,
            enabledBorder: OutlineInputBorder(
                // borderRadius: BorderRadius.all(Radius.circular(5.0)),
                borderSide: BorderSide.none
                // BorderSide(
                //   color: Colors.red, //transparent,
                //   width: 1.0,
                // )
                ),
            border: OutlineInputBorder(
                // borderRadius: BorderRadius.all(Radius.circular(5.0)),
                borderSide: BorderSide.none
                // BorderSide(
                //   color: Colors.red, //transparent,
                //   width: 1.0,
                // )
                ),
            focusedBorder: OutlineInputBorder(
                // borderRadius: BorderRadius.all(Radius.circular(5.0)),
                borderSide: BorderSide.none
                // BorderSide(
                //   color: Colors.red, //transparent,
                //   width: 1.0,
                // )
                ),
            hintStyle: TextStyle(color: Colors.grey[600], fontSize: 16.0),
            suffixIcon: Container(
              // alignment: Alignment.centerRight,
              // color: Colors.red,
              child: IconButton(
                  icon: Icon(Icons.search,
                      size: 18,
                      color: ThemeUtils.getThemeColor(
                          context: context,
                          brightnessColor: Colors.grey[400],
                          darkColor: Colors.white)),
                  onPressed: () {
                    /// 搜索状态外抛
                    if (onSearching != null) {
                      onSearching(false);
                    }
                    appBarController.stream.add(false);
                    // onChange('');
                    print('suffixIcon-onPressed');
                  }),
            ),

            // Icon(
            //   Icons.search,
            //   color: mainTextColor.withAlpha(100),
            // ),
          ),
        ),
      ),
    );
  }
}
